package com.fitness.fitness.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.fitness.convertor.CourseCommentConvertor;
import com.fitness.fitness.modal.course.dto.CourseCommentDto;
import com.fitness.fitness.modal.course.entity.CourseComment;
import com.fitness.fitness.modal.course.params.CourseCommentQueryParams;
import com.fitness.fitness.modal.course.vo.CourseCommentVO;
import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.ICourseCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/course/comment")
public class CourseCommentController {

    private final ICourseCommentService courseCommentService;
    private final CourseCommentConvertor courseCommentConvertor;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<CourseCommentVO>> page(CourseCommentQueryParams queryParams) {
        Page<CourseCommentVO> result = courseCommentService.getCourseCommentPage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param commentDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody CourseCommentDto commentDto) {
        boolean result = courseCommentService.addCourseComment(commentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("CC10001","添加失败");
    }

    /**
     * 修改
     * @param commentDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody CourseCommentDto commentDto) {
        boolean result = courseCommentService.updateCourseComment(commentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("CC10002","更新失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = courseCommentService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("C10003","删除失败");
    }

    /**
     * 获取课程表单
     */
    @GetMapping("/getCourseForm")
    public ServerResponseEntity<CourseCommentVO> getCourseCommentForm(Long id) {
        CourseComment result = courseCommentService.getById(id);
        CourseCommentVO courseCommentVO = courseCommentConvertor.entity2Vo(result);
        return ServerResponseEntity.success(courseCommentVO);
    }
}
